#include <iostream>
#include <fstream>
#include <chrono>

void performance_comparison() 
{
    // 测试1: 默认同步开启
    {
        std::ofstream file("default_sync.txt");
        auto start = std::chrono::high_resolution_clock::now();
        
        for (int i = 0; i < 100000; ++i) 
        {
            file << "Data " << i << "\n";
        }
        
        auto end = std::chrono::high_resolution_clock::now();
        auto duration = std::chrono::duration_cast<std::chrono::microseconds>(end - start);
        std::cout << "默认同步: " << duration.count() << " 微秒" << std::endl;
    }
    
    // 测试2: 关闭同步
    {
        char* buf_ptr = new char[1024 * 1024 * 8];
        std::ios_base::sync_with_stdio(false);
        std::ofstream file("no_sync.txt");
        file.rdbuf()->pubsetbuf(buf_ptr, 1024 * 1024 * 8);
        auto start = std::chrono::high_resolution_clock::now();
        
        for (int i = 0; i < 100000; ++i) 
        {
            file << "Data " << i << "\n";
        }
        
        auto end = std::chrono::high_resolution_clock::now();
        auto duration = std::chrono::duration_cast<std::chrono::microseconds>(end - start);
        delete buf_ptr;
        std::cout << "关闭同步: " << duration.count() << " 微秒" << std::endl;
    }
}

int main()
{
    performance_comparison();
    return 0;
}